<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Cookie;
use think\Db;

class Common extends Controller
{
    protected function _initialize()
    {	
    	if(!input('session.uid')) {
    		$id = $this->_checkAutoLogin();
	    	if ($id == false) {
	    		return $this->error('请先登录', 'login/index');
	    	}
	    	Session::set('uid', $id);
	    	
	    	define("UID", $id);
	    } else {
	    	define("UID", Session::get('uid'));
	    }
    }

    private function _checkAutoLogin(){	
		if (!input('cookie.autoLoginFlag')) {
			$flag = input('cookie.autoLoginFlag');
			$info = autoLoginFlagDecode($flag);
			if (count($info) == 4) {
				if ($info[2] > time() && $info[3] == WEBKEY) {
					$password = DB::name('admin')->where('username', $info['0'])->value('password');
	    			if ($password == $info['1']) {
	    				return $info['0'];
	    			}
				}
			}	
		}
    	return false;
    }
}


